package servlet;

import java.io.*;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.*;
import bll.*;
import model.*;
import dal.*;

/**
 * Servlet implementation class CompanyServlet
 */
@WebServlet(urlPatterns = { "/Purchase", "/purchase", "/PurchaseServlet",
		"/purchaseServlet" }, initParams = { @WebInitParam(name = "mode", value = "") })
public class PurchaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public PurchaseServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("new")) {

			if (RightUtil.<PurchaseModel> checkInsertRight()) {

				// TODO : MORE CODE HERE

				getServletContext().getRequestDispatcher("/purchaseNew.jsp")
						.forward(request, response);
			} else {
				// TODO no right
			}
		} else if (mode.equalsIgnoreCase("edit")) {

			PurchaseModel query = new PurchaseModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<PurchaseModel, PurchaseModelExt> dal = new GenericDal<PurchaseModel, PurchaseModelExt>(
					new PurchaseModel());

			PurchaseModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				// TODO : MORE CODE HERE

				request.setAttribute("purchase", obj);
				getServletContext().getRequestDispatcher("/purchaseEdit.jsp")
						.forward(request, response);

			} else {
				// TODO : data not found
			}
		} else if (mode.equalsIgnoreCase("view")) {

			PurchaseModel query = new PurchaseModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<PurchaseModel, PurchaseModelExt> dal = new GenericDal<PurchaseModel, PurchaseModelExt>(
					new PurchaseModel());

			PurchaseModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {
				// TODO : LOAD MASTER DATA HERE
				// It's specific code that cannot be generated

				request.setAttribute("purchase", obj);
				getServletContext().getRequestDispatcher("/purchaseView.jsp")
						.forward(request, response);

			} else {
				// TODO : data not found
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("insert")) {

			PurchaseModel obj = new PurchaseModel();

			String name = request.getParameter("name");
			obj.setName(name);

			String code = request.getParameter("code");
			obj.setCode(code);

			String description = request.getParameter("description");
			obj.setDescription(description);

			String id_task = request.getParameter("id_task");
			if (id_task != null && !id_task.isEmpty()) {
				obj.setIdTask(Integer.parseInt(id_task));
			}

			String plan_cost = request.getParameter("plan_cost");
			if (plan_cost != null && !plan_cost.isEmpty()) {
				obj.setPlanCost(Double.parseDouble(plan_cost));
			}

			String real_cost = request.getParameter("real_cost");
			if (real_cost != null && !real_cost.isEmpty()) {
				obj.setRealCost(Double.parseDouble(real_cost));
			}

			String plan_order_time_date = request
					.getParameter("plan_order_time_date");
			String plan_order_time_time = request
					.getParameter("plan_order_time_time");
			if (plan_order_time_date != null && !plan_order_time_date.isEmpty()) {
				if (plan_order_time_time != null
						&& !plan_order_time_time.isEmpty()) {
					obj.setPlanOrderTime(DateTimeUtil.parseDateTime(
							plan_order_time_date, plan_order_time_time));
				} else {
					obj.setPlanOrderTime(DateTimeUtil
							.parseDate(plan_order_time_date));
				}
			}

			String plan_delivery_time_date = request
					.getParameter("plan_delivery_time_date");
			String plan_delivery_time_time = request
					.getParameter("plan_delivery_time_time");
			if (plan_delivery_time_date != null
					&& !plan_delivery_time_date.isEmpty()) {
				if (plan_delivery_time_time != null
						&& !plan_delivery_time_time.isEmpty()) {
					obj.setPlanDeliveryTime(DateTimeUtil.parseDateTime(
							plan_delivery_time_date, plan_delivery_time_time));
				} else {
					obj.setPlanDeliveryTime(DateTimeUtil
							.parseDate(plan_delivery_time_date));
				}
			}

			String real_order_tate_date = request
					.getParameter("real_order_tate_date");
			String real_order_tate_time = request
					.getParameter("real_order_tate_time");
			if (real_order_tate_date != null && !real_order_tate_date.isEmpty()) {
				if (real_order_tate_time != null
						&& !real_order_tate_time.isEmpty()) {
					obj.setRealOrderTate(DateTimeUtil.parseDateTime(
							real_order_tate_date, real_order_tate_time));
				} else {
					obj.setRealOrderTate(DateTimeUtil
							.parseDate(real_order_tate_date));
				}
			}

			String real_delivery_time_date = request
					.getParameter("real_delivery_time_date");
			String real_delivery_time_time = request
					.getParameter("real_delivery_time_time");
			if (real_delivery_time_date != null
					&& !real_delivery_time_date.isEmpty()) {
				if (real_delivery_time_time != null
						&& !real_delivery_time_time.isEmpty()) {
					obj.setRealDeliveryTime(DateTimeUtil.parseDateTime(
							real_delivery_time_date, real_delivery_time_time));
				} else {
					obj.setRealDeliveryTime(DateTimeUtil
							.parseDate(real_delivery_time_date));
				}
			}

			String need_approval = request.getParameter("need_approval");
			obj.setNeedApproval(need_approval.equalsIgnoreCase("true")
					|| need_approval.equalsIgnoreCase("1"));

			String status = request.getParameter("status");
			if (status != null && !status.isEmpty()) {
				obj.setStatus(Integer.parseInt(status));
			}

			String is_draft = request.getParameter("is_draft");
			obj.setIsDraft(is_draft.equalsIgnoreCase("true")
					|| is_draft.equalsIgnoreCase("1"));

			String notification = request.getParameter("notification");
			if (notification != null && !notification.isEmpty()) {
				obj.setNotification(Integer.parseInt(notification));
			}

			obj.setCreator(SessionUtil.getCurrentUserID());

			if (CommonBll.checkInputs(obj)) {
				GenericDal<PurchaseModel, PurchaseModelExt> dal = new GenericDal<PurchaseModel, PurchaseModelExt>(
						new PurchaseModel());

				int res = 0;
				try {
					res = dal.insert(obj);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (res > 0) {
					// TODO : success, redirect to somewhere
				} else {
					// TODO : processing error here
				}
			} else {

				// invalid input

				request.setAttribute("purchase", obj);
				getServletContext().getRequestDispatcher("/purchaseInput.jsp")
						.forward(request, response);

			}
		} else if (mode.equalsIgnoreCase("update")) {

			PurchaseModel query = new PurchaseModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<PurchaseModel, PurchaseModelExt> dal = new GenericDal<PurchaseModel, PurchaseModelExt>(
					new PurchaseModel());

			PurchaseModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				String name = request.getParameter("name");
				obj.setName(name);

				String code = request.getParameter("code");
				obj.setCode(code);

				String description = request.getParameter("description");
				obj.setDescription(description);

				String id_task = request.getParameter("id_task");
				obj.setIdTask(Integer.parseInt(id_task));

				String plan_cost = request.getParameter("plan_cost");
				obj.setPlanCost(Double.parseDouble(plan_cost));

				String real_cost = request.getParameter("real_cost");
				obj.setRealCost(Double.parseDouble(real_cost));

				String plan_order_time_date = request
						.getParameter("plan_order_time_date");
				String plan_order_time_time = request
						.getParameter("plan_order_time_time");
				obj.setPlanOrderTime(DateTimeUtil.parseDateTime(
						plan_order_time_date, plan_order_time_time));

				String plan_delivery_time_date = request
						.getParameter("plan_delivery_time_date");
				String plan_delivery_time_time = request
						.getParameter("plan_delivery_time_time");
				obj.setPlanDeliveryTime(DateTimeUtil.parseDateTime(
						plan_delivery_time_date, plan_delivery_time_time));

				String real_order_tate_date = request
						.getParameter("real_order_tate_date");
				String real_order_tate_time = request
						.getParameter("real_order_tate_time");
				obj.setRealOrderTate(DateTimeUtil.parseDateTime(
						real_order_tate_date, real_order_tate_time));

				String real_delivery_time_date = request
						.getParameter("real_delivery_time_date");
				String real_delivery_time_time = request
						.getParameter("real_delivery_time_time");
				obj.setRealDeliveryTime(DateTimeUtil.parseDateTime(
						real_delivery_time_date, real_delivery_time_time));

				String need_approval = request.getParameter("need_approval");
				obj.setNeedApproval(need_approval.equalsIgnoreCase("true")
						|| need_approval.equalsIgnoreCase("1"));

				String status = request.getParameter("status");
				obj.setStatus(Integer.parseInt(status));

				String is_draft = request.getParameter("is_draft");
				obj.setIsDraft(is_draft.equalsIgnoreCase("true")
						|| is_draft.equalsIgnoreCase("1"));

				String notification = request.getParameter("notification");
				obj.setNotification(Integer.parseInt(notification));

				if (CommonBll.checkInputs(obj)) {

					int res = Integer.MIN_VALUE;
					try {
						res = dal.update(obj);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					if (res == 0) {
						// TODO success
					} else {
						// TODO error
					}
				} else {
					request.setAttribute("purchase", obj);
					getServletContext().getRequestDispatcher(
							"/purchaseEdit.jsp").forward(request, response);
				}
			} else {
				// TODO data not found
			}
		} else if (mode.equalsIgnoreCase("delete")) {

			PurchaseModel query = new PurchaseModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<PurchaseModel, PurchaseModelExt> dal = new GenericDal<PurchaseModel, PurchaseModelExt>(
					new PurchaseModel());

			int res = Integer.MIN_VALUE;
			try {
				res = dal.delete(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (res == 0) {
				// TODO success
			} else {
				// TODO fail
			}
		}
	}

}